// pages/my/manage/manage.js
var app = getApp()
var util = require('../../../utils/util.js')
var date;
var user_visitors
var address
var permission
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:'2020-02-16',
    recordaddress:'',
    permission:'10',
    fileUrl:'',
    flieID_state:0,
    user:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.address)
    console.log(options.permission)
    permission = options.permission
    address = options.address
    let that=this;
    let realtime = util.formatTime(new Date());
    date = realtime.substring(0, 4) + "-" + realtime.substring(5, 7) + "-" + realtime.substring(8, 10)
    if (permission == 1 | permission==10){
      that.setData({
        recordaddress: options.address
      })
    }
    that.setData({
      date: date,
      permission: options.permission,
      userInfo: app.globalData.userInfo
    })
  },

  //设置日期
  DateChange(e) {
    date = e.detail.value
    this.setData({
      date: e.detail.value
    })
  },
  //设置查询地点
  address: function (e) {
    address = e.detail.value
    this.setData({
      recordAddress: e.detail.value,
    })
  },
  //查询数据，在前端显示基本信息
  chaxun:function(){
    let that = this
    if (permission != '00'){
    }else{
      address = that.data.recordAddress
    }
    
    wx.cloud.callFunction({
      name: 'getData',
      data: {
        recordAddress: address,
        date: that.data.date
      },
      success: res => {
        // console.log('[云函数] [login] user openid: ', res.result)
        user_visitors = res.result.data
        // console.log(user_visitors)
        that.setData({
          user: res.result.data
        })
      },
      fail: res=>{
        console.log("云函数调用失败")
        wx.showModal({
          title: '查询失败',
          content: '请核对查询选项后重新查询',
        })
      }
    })
  },
  //导出数据，将导出的数据保存在云存储
  daochu:function(){
    var that =this
    let excelname = "excel/"+address+"("+ date +")"+".xlsx" //设置excel文件名,存储路径
    console.log(excelname)
    if (user_visitors == undefined){
      wx.showModal({
        title: '暂无数据',
        content: '请先点击查询，然后再点击导出',
      })
    }else{
      wx.showToast({
        title: 'excel生成中...',
        mask: true,
        icon: 'loading'
      })
      let row_out = [['姓名', '身份证号码', '电话号码', '车牌号', '体温', '身体状况', '去向', '外出原因', '家庭地址', '日期', '时间'],[]]; //表属性
      let row_in  = [['姓名', '身份证号码', '电话号码', '车牌号', '体温', '身体状况', '从何处来', '到此地原因', '家庭地址', '日期', '时间']]; //表属性
      let in_index = 0;
      let out_index =0;
      for (let i = 0; i < user_visitors.length;i++){
        if (user_visitors[i].direction =='出去'){      //离开小区表格处理
          let arr = [];
          arr[0] = user_visitors[i].name;
          arr[1] = user_visitors[i].idcard;
          arr[2] = user_visitors[i].phone;
          arr[3] = user_visitors[i].carnumber;
          arr[4] = user_visitors[i].temp;
          arr[5] = user_visitors[i].health;
          arr[6] = user_visitors[i].destination;
          arr[7] = user_visitors[i].reason;
          arr[8] = user_visitors[i].address;
          arr[9] = user_visitors[i].date;
          arr[10] = user_visitors[i].time;
          row_out[out_index+1] = arr
          out_index++
        } 
        else if (user_visitors[i].direction == '进入'){  //进入小区表格处理
          let arr = [];
          arr[0] = user_visitors[i].name;
          arr[1] = user_visitors[i].idcard;
          arr[2] = user_visitors[i].phone;
          arr[3] = user_visitors[i].carnumber;
          arr[4] = user_visitors[i].temp;
          arr[5] = user_visitors[i].health;
          arr[6] = user_visitors[i].destination;
          arr[7] = user_visitors[i].reason;
          arr[8] = user_visitors[i].address;
          arr[9] = user_visitors[i].date;
          arr[10] = user_visitors[i].time;
          row_in[in_index+1] = arr
          in_index++;
        }
      }
      wx.cloud.callFunction({
        name: 'excel',
        data: {
          row_out:row_out,
          row_in: row_in,
          excelname: excelname
        },
        success: res => {
          // fileID = res.result
          // console.log(user_visitors)
          wx.cloud.getTempFileURL({
            fileList: [res.result],
            success: res => {
              // get temp file URL
              console.log("文件下载链接")
              console.log( res.fileList[0].tempFileURL)
              wx.showToast({
                title: '请点击复制链接复制到浏览器下载excel',
                mask: true,
                // icon: 'success',
                duration:3000
              })
              that.setData({
                flieID_state: 1,
                fileUrl: res.fileList[0].tempFileURL
              })
            },
            fail: err => {
            }
          })
        },
        fail: res => {
          console.log("云函数调用失败")
        }
      })
    }
  },
  copyurl:function(){
    let that = this
    wx.setClipboardData({
      data: that.data.fileUrl,
      success(res) {
        console.log(that.data.fileUrl)
        wx.getClipboardData({
          success(res) {
            // console.log("复制成功", res.data) // data
          }
        })
      }
    })
  }
})